package com.xzr.yespaper.controller;

import com.xzr.yespaper.bean.Paper;
import com.xzr.yespaper.bean.User;
import com.xzr.yespaper.service.PaperService;
import com.xzr.yespaper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class UserMainController {

    @Autowired
    UserService userService;

    @Autowired
    PaperService paperService;

    /**
     * To main page
     * @return index.html
     */
    @GetMapping(value = {"/","/index"})
    public String mainPage(){
        return "index";
    }

    /**
     * To main page
     * @return index.html
     */
    @GetMapping("/logout")
    public String logout(HttpSession session){
        // 清空session
        session.invalidate();
        return "index";
    }

    /**
     * Pass value to search page
     * @return articles.html
     */
    @PostMapping("/search")
    @ResponseBody
    public String search(@RequestParam("content") String content, @RequestParam("type") String type, HttpSession session){
        Long now = new Date().getTime();
        session.setAttribute("searchcontent",content);
        session.setAttribute("searchtype",type);
        session.setAttribute("searchtime",now);
        return "";
    }

    /**
     * To paper page
     * @return articles.html
     */
    @GetMapping("/searchpage")
    public String searchPage(HttpSession session){
        try{
            Long now = new Date().getTime();
            Long dd = now - Long.parseLong(session.getAttribute("searchtime").toString());
            // System.out.println(dd);
            if(dd > 5000){
                session.setAttribute("searchcontent","");
                session.setAttribute("searchtype","");
            }
        }catch (Exception e){
            System.out.print(e);
            session.setAttribute("searchcontent","");
            session.setAttribute("searchtype","");
        }
        return "articles";
    }

    /**
     * 登陆验证
     * 登陆成功 @return 请求转发
     * 登陆失败 @return index.html
     */
    @PostMapping("/login")
    public String doLogin(User user, HttpSession session, Model model) {
        try{
            User loginUser = userService.getUserByUsername(user.getUsername());
            if(loginUser.getPassword().equals(user.getPassword())){
                // make session available
                session.setAttribute("user",loginUser);
                session.setAttribute("username",loginUser.getUsername());
                session.setAttribute("userid",loginUser.getId());
                return "redirect:/personal";
            } else {
                model.addAttribute("message","Username or Password is Wrong!");
                return "index";
            }
        }catch (Exception e){
            model.addAttribute("message","Username or Password is Wrong!");
            return "index";
        }
    }

    /**
     * 个人页信息的加载
     * @return personal.html
     */
    @GetMapping("/personal")
    public String personal(HttpSession session, Model model) {
        if(session.getAttribute("user")!=null){
            String loginUser = session.getAttribute("username").toString();
            // 通过user得到paper
            List<Paper> ownpaper = paperService.getPaperByAuthor(loginUser);
            List<Paper> ownpublishpaper = paperService.getPaperByAuthor2(loginUser);
            List<Paper> revpaper = paperService.getPaperByReviewer("%"+loginUser+"%");
//            System.out.println(revpaper);
            session.setAttribute("ownpaper",ownpaper);
            session.setAttribute("ownpubpaper",ownpublishpaper);
            session.setAttribute("revpaper",revpaper);
            session.setAttribute("papernum",ownpublishpaper.size());
            return "personal";
        }else {
            model.addAttribute("message","Please login first!");
            return "index";
        }
    }

    /**
     * 测试用例 just for test
     * @param username
     * @return
     */
    @GetMapping("/acct")
    public User getByName(@RequestParam("username") String username){
        return userService.getUserByUsername(username);
    }
}
